import Vue 			from 'vue'
import util 		from '../../util'
import axios 		from 'axios'
import { Toast,Dialog }    from 'vant';
Vue.use(Toast,Dialog);

export default {
	name: 'sellType',
	data(){
		return {
			name:'',				//购买的币种名称
			account:0,				//购买的数量
			OTCWorkTime:'',			//服务在线时间
			list:[],
			token:'',
			ajaxBtn:false,			//接口是否完全返回
			EOSAccount:'',			//EOS账号
			ETHAccount:'',			//ETH账号
		}
	},
	computed:{
		isIphoneX(){
			return util.isIphoneX()
		},
		isIphone(){
			return util.client()
		},
		isMYKEY(){
			return util.isMYKEY()
		}
	},
	methods:{
		goChoice(_item){
			let self = this;
			//跳转
			if(!this.ajaxBtn){
				Toast('请耐心等待接口返回');
				return
			}
			if(_item.onSet == 2){
				//首次设置支付方式
				this.$router.push({
					path:'/setUserDropInfo',
					query:{
						type:_item.type
					}
				})
			}else{
				//跳转到出售页面
				if(!_item.type || !this.name || !this.account || !_item.id){
					Toast('数据格式错误，请刷新重试')
					return
				}
				this.$router.replace({
					path:'/sell',
					query:{
						type:_item.type,
						name:this.name,
						account:this.account,
						accountId:_item.id,
						death:1
					}
				})
			}
		},
		otcPayConfig(){
			//获取允许的支付方式
			return new Promise((resolve,reject) => {
				let params = {};
				if(this.isMYKEY) params.token = this.token || util.getToken();
				util.fetch(util.ajaxUrl()+`/mykey/otc-pay-config`,params).then(res => {
					if(res.retCode === 0){
						this.OTCWorkTime = res.retData.otc_serve_desc;
						util.setStorage('OTCWorkTime',this.OTCWorkTime)
						resolve(res.retData.recieve)
					}else{
						Toast(res.retMsg || '未知错误')
					}
					reject('')
				}).catch(error => {
					reject('')
					console.log(error)
				})
			})
		},
		payMethods(){
			//判断支付方式是否配置信息
			return new Promise((resolve,reject) => {
				util.fetch(util.ajaxUrl()+`/mykey/pay-methods`,{
					token:this.token || util.getToken()
				}).then(res => {
					if(res.retCode === 0){
						if(res.retData && res.retData.payMethods && Array.isArray(res.retData.payMethods)){
							resolve(res.retData.payMethods)
						}else{
							Toast('数据格式错误')
						}
					}else{
						Toast(res.retMsg || '未知错误')
					}
					reject('')
				}).catch(error => {
					reject('')
					console.log(error)
				})
			})
		},
		init(){
			//先渲染可能的缓存数据
			let sellType = util.getStorage('sellType');
			if(sellType) {
				this.list = JSON.parse(sellType);
				this.OTCWorkTime = util.getStorage('OTCWorkTime');
			};
			//拉取后台接口再渲染页面
			Promise.all([this.otcPayConfig(),this.payMethods()]).then(res => {
				util.bridgeData(res[0]).then(_list => {
					// _list为后端配置的支持的付款方式
					_list.map(item => {
						let _filter = res[1].filter(ele => ele.pay_type == item.type);
						item.onSet  = _filter.length > 0?1:2;
						item.id     = _filter.length > 0?_filter[0].id:null;
					})
					this.list = _list;
					this.ajaxBtn = true;
					util.setStorage('sellType',_list)
				})
			}).catch(error => {
				Toast('数据异常，请刷新重试')
			})
		},
		goBack(){
			util.navigateBack(-1,this)
		}
	},
	created() {
		let self = this;
		util.waitSDKReady().then(token => {
			this.token   = token;
			this.name    = this.$route.query.name;
			this.account = this.$route.query.account;
			this.EOSAccount = util.getStorage('EOSAccount');
			this.ETHAccount = util.getStorage('ETHAccount');
			this.init();
		}).catch((e) => {
			Toast('获取token失败')
		})
		setTimeout(() => {
			util.registerHandler('refreshCommonRequestWhenGoBackView',res => {
				self.init();
			})
			//退出登录的时候native调用H5方法清除token缓存
			util.registerHandler('loginOutDeleteToken',res => {
				window.token = '';
				this.token = '';
				localStorage.removeItem('token');
			})
			util.callHandler('setStatusBarStyle',{
				style:'dark'
			})
		},500)
    }
}